package com.zmn.mcc.dubbo.impl.menu;

import com.zmn.base.plat.engine.common.constant.DubboConsts;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.mcc.common.constant.MenuConsts;
import com.zmn.mcc.common.dto.menu.McMenuDRO;
import com.zmn.mcc.dubbo.interfaces.menu.MenuListRemoteService;
import com.zmn.mcc.model.entity.menu.McMenu;
import com.zmn.mcc.model.entity.menu.McMenuQuery;
import com.zmn.mcc.model.entity.sys.McSys;
import com.zmn.mcc.persistence.interfaces.menu.MenuDao;
import com.zmn.mcc.services.interfaces.menu.MenuService;
import com.zmn.mcc.services.interfaces.sys.SysService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 描述: 菜单远程查询接口实现类
 *
 * @author guoqiao
 * @since 2021/6/2 17:27
 */
@Slf4j
@Service(version = DubboConsts.INTERFACE_VERSION, validation = "true")
public class MenuListRemoteServiceImpl implements MenuListRemoteService {

    @Resource
    private MenuService menuService;

    @Resource
    private SysService sysService;

    @Resource
    private MenuDao menuDao;

    @Override
    public ResponseDTO<List<McMenuDRO>> listAllMenu() {

        McMenuQuery query = McMenuQuery.builder().status(GlobalConsts.YES).build();
        return ResponseDTO.success(listMenuDROByQuery(query));
    }

    @Override
    public ResponseDTO<List<McMenuDRO>> listAllMenu_copy() {

        McMenuQuery query = McMenuQuery.builder().status(GlobalConsts.YES).build();

        // 查询所有系统的域名
        Map<Integer, String> urlMap = Optional.ofNullable(sysService.listAll(GlobalConsts.YES)).orElse(
                Collections.emptyList()).stream().collect(Collectors.toMap(McSys::getSysId, McSys::getUrl));
        // 查询有效菜单

        List<McMenu> menuList = Optional.ofNullable(menuDao.listByQuery_copy(query)).orElse(Collections.emptyList())
                .stream().peek(menu -> {
                    if (MenuConsts.MENU_TYPE_MENU_KEY == menu.getMenuType() && NumberUtil.isNotNullOrZero(
                            menu.getSysId())) {
                        // url设置域名
                        menu.setMenuUrl(urlMap.get(menu.getSysId()) + menu.getMenuUrl());
                    }
                }).collect(Collectors.toList());
        return ResponseDTO.success(BeanMapper.mapList(menuList, McMenuDRO.class));
    }

    @Override
    public ResponseDTO<List<McMenuDRO>> listMenuByShowSysId(Integer showSysId) {

        McMenuQuery query = McMenuQuery.builder().status(GlobalConsts.YES).showSysId(showSysId).build();
        return ResponseDTO.success(listMenuDROByQuery(query));
    }

    /**
     * 描述: 筛选菜单列表
     *
     * @param query
     * @return java.util.List<com.zmn.mcc.common.dto.menu.McMenuDRO>
     * @author jiafei
     * @since 2021/10/9 15:15
     */
    private List<McMenuDRO> listMenuDROByQuery(McMenuQuery query) {

        // 查询所有系统的域名
        Map<Integer, String> urlMap = Optional.ofNullable(sysService.listAll(GlobalConsts.YES)).orElse(
                Collections.emptyList()).stream().collect(Collectors.toMap(McSys::getSysId, McSys::getUrl));
        // 查询有效菜单

        List<McMenu> menuList = Optional.ofNullable(menuService.listByQuery(query)).orElse(Collections.emptyList())
                                        .stream().peek(menu -> {
                    if (MenuConsts.MENU_TYPE_MENU_KEY == menu.getMenuType() && NumberUtil.isNotNullOrZero(
                            menu.getSysId())) {
                        // url设置域名
                        menu.setMenuUrl(urlMap.get(menu.getSysId()) + menu.getMenuUrl());
                    }
                }).collect(Collectors.toList());
        return BeanMapper.mapList(menuList, McMenuDRO.class);
    }

}
